<?php
/**
 * 开放平台基础类文件 授权及消息通知的实现
 */
namespace app\common\helper\upload;
use OSS\OssClient;
class UploadAliHandler{
    
    private $config;
    private $url;
    public function __construct($config) {
        $this->config = $config;
        $this->url = rtrim($config['domain'],'\/');
    }
    
    public function upload($file,$name){
        $accessKeyId = trim($this->config['ali']['access_key_id']);
        $accessKeySecret = trim($this->config['ali']['access_key_secret']);
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = trim($this->config['ali']['endpoint']);
        // 设置存储空间名称。
        $bucket=  trim($this->config['ali']['bucket']);
        // 设置文件名称。
        $object = $name;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
        $filePath = $file['tmp_name'];
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $ossClient->uploadFile($bucket, $object, $filePath);        
        return [
            'url'   => $this->url . '/'.$name,
            'key'   => $name,
            'hash'  => md5($name), //其实应该md5 file的
        ];
    }
    
    public function delete(){
        
    }
    
}